package org.eventexample.ws;

import org.eventexample.www.events.Event;
import org.eventexample.www.events.EventsArray;
import org.eventexample.www.events.GetAllEvents;
import org.eventexample.www.events.GetAllEventsFault;
import org.eventexample.www.events.GetAllEventsResponse;
import org.eventexample.www.events.GetEventsByDate;
import org.eventexample.www.events.GetEventsByDateFault;
import org.eventexample.www.events.GetEventsByDateResponse;
import org.eventexample.www.events.GetEventsByKeyword;
import org.eventexample.www.events.GetEventsByKeywordFault;
import org.eventexample.www.events.GetEventsByKeywordResponse;
import org.eventexample.ws.ErrorMessages;
import org.eventexample.ws.ServiceUnavailableException;


public class EventBusiness implements EventsSkeletonInterface
{

	@Override
	public GetEventsByKeywordResponse getEventsByKeyword(GetEventsByKeyword getEventsByKeyword) throws GetEventsByKeywordFaultException 
	{
		try
		{
		GetEventsByKeywordResponse object = new GetEventsByKeywordResponse();
		
		String keyword = (String)getEventsByKeyword.getKeyword();
		
		EventsDAO dao = new EventsDAO();
		Event[] eventsResult = dao.getEventsByKeyword(keyword);
		
		EventsArray array = new EventsArray();
		array.setEvent(eventsResult);
		object.setOut(array);
		return object;
		}
		catch(ServiceUnavailableException ex)
		{
			GetEventsByKeywordFault keywordFault = new GetEventsByKeywordFault();
			keywordFault.setGetEventsByKeywordFault(ex.toString());
			GetEventsByKeywordFaultException error = new GetEventsByKeywordFaultException();
			error.setFaultMessage(keywordFault);
			throw error;	
		}catch(BlankException ex)
		{
			GetEventsByKeywordFault keywordFault = new GetEventsByKeywordFault();
			keywordFault.setGetEventsByKeywordFault(ex.toString());
			GetEventsByKeywordFaultException error = new GetEventsByKeywordFaultException();
			error.setFaultMessage(keywordFault);
			throw error;
		}
		catch(Exception e)
		{
			GetEventsByKeywordFault keywordFault = new GetEventsByKeywordFault();
			keywordFault.setGetEventsByKeywordFault(ErrorMessages.ServiceUnavailableError);
			GetEventsByKeywordFaultException error = new GetEventsByKeywordFaultException();
			error.setFaultMessage(keywordFault);
			throw error;
		}
	}

	@Override
	public GetEventsByDateResponse getEventsByDate(GetEventsByDate getEventsByDate) throws GetEventsByDateFaultException 
	{
		try
		{
		GetEventsByDateResponse object = new GetEventsByDateResponse();	
		String startDate = (String)getEventsByDate.getStart();
		String endDate = (String)getEventsByDate.getEnd();
		
		EventsDAO dao = new EventsDAO();
		Event[] eventsResult = dao.getEventsByDate(startDate, endDate);
		
		EventsArray array = new EventsArray();
		array.setEvent(eventsResult);
		object.setOut(array);
		return object;
		}
		catch(ServiceUnavailableException ex)
		{
		GetEventsByDateFault dateFault = new GetEventsByDateFault();
		dateFault.setGetEventsByDateFault(ex.toString());
		GetEventsByDateFaultException error = new GetEventsByDateFaultException();
		error.setFaultMessage(dateFault);
		throw error;
		}
		catch(BlankException ex)
		{
			GetEventsByDateFault dateFault = new GetEventsByDateFault();
			dateFault.setGetEventsByDateFault(ex.toString());
			GetEventsByDateFaultException error = new GetEventsByDateFaultException();
			error.setFaultMessage(dateFault);
			throw error;
		}
		catch(DateNotValidException ex)
		{
			GetEventsByDateFault dateFault = new GetEventsByDateFault();
			dateFault.setGetEventsByDateFault(ex.toString());
			GetEventsByDateFaultException error = new GetEventsByDateFaultException();
			error.setFaultMessage(dateFault);
			throw error;
		}
		catch(Exception e)
		{
			GetEventsByDateFault dateFault = new GetEventsByDateFault();
			dateFault.setGetEventsByDateFault(e.toString());
			GetEventsByDateFaultException error = new GetEventsByDateFaultException();
			error.setFaultMessage(dateFault);
			throw error;
		}
	}

	@Override
	public GetAllEventsResponse getAllEvents(GetAllEvents getAllEvents) throws GetAllEventsFaultException 
	{	
		try
		{
		GetAllEventsResponse object = new GetAllEventsResponse();
		
		EventsDAO dao = new EventsDAO();
		Event[] eventsResult = dao.getAllEvents();
		
		EventsArray array = new EventsArray();
		array.setEvent(eventsResult);
		object.setOut(array);
		
		return object;
		}
		catch(Exception e)
		{
			GetAllEventsFault dateFault = new GetAllEventsFault();
			dateFault.setGetAllEventsFault(ErrorMessages.ServiceUnavailableError);
			GetAllEventsFaultException error = new GetAllEventsFaultException();
			error.setFaultMessage(dateFault);
			throw error;
		}
	}


}
